<template>
  <div class="login-container">
    <el-card class="login-card">
      <div slot="header">
        <h2>系统登录</h2>
      </div>
      <el-form :model="form" :rules="rules" ref="loginForm" @submit.native.prevent="onLogin">
        <el-form-item prop="username">
          <el-input 
            v-model="form.username" 
            prefix-icon="el-icon-user"
            placeholder="请输入用户名">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="form.password" 
            prefix-icon="el-icon-lock"
            type="password" 
            placeholder="请输入密码">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit" :loading="loading" class="login-button">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    onLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // 模拟登录请求
          setTimeout(() => {
            this.$store.dispatch('user/login', { 
              token: '123', 
              role: 'admin' 
            }).then(() => {
              // 登录成功后，确保状态更新完成再进行路由跳转
              this.$nextTick(() => {
                const redirectPath = this.$route.query.redirect || '/'
                this.$router.replace(redirectPath)
              })
            }).finally(() => {
              this.loading = false
            })
          }, 1000)
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  /* background-color: #f3f3f3; */
  background-image: url('~@/assets/img1.jpg'); /* 请替换为您的图片路径 */
  background-size: cover;
  background-position: center;
}

.login-card {
  width: 400px;
}

.login-button {
  width: 100%;
}

h2 {
  text-align: center;
  margin: 0;
  color: #303133;
}
</style> 